package com.bs.medicine.struts.action;
import javax.servlet.http.HttpSession;

import org.apache.axis2.AxisFault;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.struts2.ServletActionContext;

import com.bs.entity.User;
import com.bs.medicine.utils.DetailLogger;
import com.bs.medicine.utils.DetailLoggerFactory;
import com.mysoap.medicine.service.TUserQuerySoapStub;
import com.mysoap.medicine.service.TUserQuerySoapStub.UserQueryRequest;
import com.mysoap.medicine.service.TUserQuerySoapStub.UserQueryRequestResponse;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 用户登录的action类
 * @author 
 *
 */

public class LoginAction extends ActionSupport{
	private User user;
	
	private static TUserQuerySoapStub tUserQuerySoapStub;

	private static DetailLogger logger = DetailLogger.getDetailLogger(LoginAction.class);
	static{
		try {
			String address="http://localhost:8080/MySoap/services/TUserQuerySoap";
		 tUserQuerySoapStub = new TUserQuerySoapStub (address);
		} catch (AxisFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
    
	/**
	 * 验证用户登录问题，登录成功后将用户信息保存到session中
	 */
	@Override
	public String execute() throws Exception {
		TUserQuerySoapStub.UserQueryRequestE userQueryRequestE = new TUserQuerySoapStub.UserQueryRequestE();
		UserQueryRequest userQueryRequest = new UserQueryRequest();
		userQueryRequest.setLoginName(user.getLoginName());
		userQueryRequest.setUpdateFlag(1);
		userQueryRequest.setUserPassword(user.getUserPassword());
		userQueryRequestE.setUserQueryRequest(userQueryRequest);
		logger.info("用户登录信息查询请求："+ToStringBuilder.reflectionToString(userQueryRequest,ToStringStyle.MULTI_LINE_STYLE));
		UserQueryRequestResponse response = tUserQuerySoapStub.userQueryRequest(userQueryRequestE);
		logger.info("用户登录信息查询请求："+ToStringBuilder.reflectionToString(response.get_return(),ToStringStyle.MULTI_LINE_STYLE));
		System.out.println(response.get_return().getResultCode());
		if(response.get_return().getResultCode()==0){
			HttpSession  session =ServletActionContext.getRequest().getSession();
			session.setAttribute("user", user);
			session.setMaxInactiveInterval(60*60*2);
			return SUCCESS;
		}else {
			return LOGIN;
		}
		
		
	}

}
